# Stage 1: Build
FROM node:20.8.1 AS builder

# Set the working directory
WORKDIR /app

ARG PORT
ARG VITE_WEB_API_URL
ARG VITE_WEB_API_TIMEOUT
ARG VITE_WEB_ENABLE_MIX_PANEL
ARG VITE_WEB_MIX_PANEL_TOKEN
ARG VITE_WEB_SPARROW_SUPPORT_EMAIL
ARG VITE_WEB_AUTH_URL
ARG VITE_WEB_SPARROW_GITHUB
ARG VITE_WEB_SPARROW_DOWNLOAD_LINK
ARG VITE_WEB_RELEASE_NOTES_API
ARG VITE_WEB_AZURE_CDN_URL
ARG VITE_WEB_BASE_URL
ARG VITE_WEB_SPARROW_LINKEDIN
ARG VITE_WEB_AZURE_INSIGHTS_CONNECTION_STRING
ARG VITE_WEB_MARKETING_URL
ARG VITE_WEB_SPARROW_DOCS
ARG VITE_WEB_PROXY_SERVICE
ARG VITE_WEB_SOCKET_IO_API_URL
ARG VITE_WEB_SPARROW_AI_WEBSOCKET
ARG VITE_WEB_APP_ENVIRONMENT_PATH
ARG VITE_WEB_CANNY_FEEDBACK_URL
ARG VITE_WEB_SPARROW_WEB_APP_URL
ARG VITE_WEB_SENTRY_DSN
ARG VITE_WEB_APP_ENVIRONMENT
ARG VITE_WEB_POSTHOG_CONNECTION_API_KEY
ARG VITE_WEB_POSTHOG_API_URL
ARG VITE_WEB_SPARROW_ADMIN_URL

ENV PORT=$PORT
ENV VITE_WEB_API_URL=$VITE_WEB_API_URL
ENV VITE_WEB_API_TIMEOUT=$VITE_WEB_API_TIMEOUT
ENV VITE_WEB_ENABLE_MIX_PANEL=$VITE_WEB_ENABLE_MIX_PANEL
ENV VITE_WEB_MIX_PANEL_TOKEN=$VITE_WEB_MIX_PANEL_TOKEN
ENV VITE_WEB_SPARROW_SUPPORT_EMAIL=$VITE_WEB_SPARROW_SUPPORT_EMAIL
ENV VITE_WEB_AUTH_URL=$VITE_WEB_AUTH_URL
ENV VITE_WEB_SPARROW_GITHUB=$VITE_WEB_SPARROW_GITHUB
ENV VITE_WEB_SPARROW_DOWNLOAD_LINK=$VITE_WEB_SPARROW_DOWNLOAD_LINK
ENV VITE_WEB_RELEASE_NOTES_API=$VITE_WEB_RELEASE_NOTES_API
ENV VITE_WEB_AZURE_CDN_URL=$VITE_WEB_AZURE_CDN_URL
ENV VITE_WEB_BASE_URL=$VITE_WEB_BASE_URL
ENV VITE_WEB_SPARROW_LINKEDIN=$VITE_WEB_SPARROW_LINKEDIN
ENV VITE_WEB_AZURE_INSIGHTS_CONNECTION_STRING=$VITE_WEB_AZURE_INSIGHTS_CONNECTION_STRING
ENV VITE_WEB_MARKETING_URL=$VITE_WEB_MARKETING_URL
ENV VITE_WEB_SPARROW_DOCS=$VITE_WEB_SPARROW_DOCS
ENV VITE_WEB_PROXY_SERVICE=$VITE_WEB_PROXY_SERVICE
ENV VITE_WEB_SOCKET_IO_API_URL=$VITE_WEB_SOCKET_IO_API_URL
ENV VITE_WEB_SPARROW_AI_WEBSOCKET=$VITE_WEB_SPARROW_AI_WEBSOCKET
ENV VITE_WEB_APP_ENVIRONMENT_PATH=$VITE_WEB_APP_ENVIRONMENT_PATH
ENV VITE_WEB_CANNY_FEEDBACK_URL=$VITE_WEB_CANNY_FEEDBACK_URL
ENV VITE_WEB_SPARROW_WEB_APP_URL=$VITE_WEB_SPARROW_WEB_APP_URL
ENV VITE_WEB_SENTRY_DSN=$VITE_WEB_SENTRY_DSN
ENV VITE_WEB_APP_ENVIRONMENT=$VITE_WEB_APP_ENVIRONMENT
ENV VITE_WEB_POSTHOG_CONNECTION_API_KEY=$VITE_WEB_POSTHOG_CONNECTION_API_KEY
ENV VITE_WEB_POSTHOG_API_URL=$VITE_WEB_POSTHOG_API_URL
ENV VITE_WEB_SPARROW_ADMIN_URL=$VITE_WEB_SPARROW_ADMIN_URL

# Copy files and build the web app
COPY package.json yarn.lock ./

# Copy packages folder
COPY packages ./packages

# Copy apps folder
COPY apps/@sparrow-web ./apps/@sparrow-web

# Install dependencies (using yarn to respect the workspace setup)
RUN yarn install --frozen-lockfile

# Build the web app
WORKDIR /app/apps/@sparrow-web
RUN yarn build

# Stage 2: Serve
FROM nginx:alpine AS runner

# Set working directory in Nginx
WORKDIR /usr/share/nginx/html

# Copy build output from builder stage
COPY --from=builder /app/apps/@sparrow-web/dist /usr/share/nginx/html

COPY ./apps/@sparrow-web/nginx.conf /etc/nginx/nginx.conf

# Expose port 80
EXPOSE 80

# Start Nginx server
CMD ["nginx", "-g", "daemon off;"]